package com.zz.server.config;

import com.zz.common.utils.ObjectUtil;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 通道缓存
 */
@Slf4j
public class ChannelCache {

    /**
     * server 客户端映射通道缓存
     */
    private final static Map<String, Channel> domainChannelCache = new ConcurrentHashMap<>();

    public static void addDomainChannelCache(String domain, Channel channel) {
        if (ObjectUtil.isEmptys(domain, channel)) {
            return;
        }
        domainChannelCache.put(domain, channel);
    }

    public static void addDomainChannelCache(List<String> domains, Channel channel) {
        if (ObjectUtil.isEmptys(domains)) {
            return;
        }
        domains.forEach(domain -> addDomainChannelCache(domain, channel));
    }

    public static Channel getChannelByDomain(String domain) {
        if (ObjectUtil.isEmpty(domain)) {
            return null;
        }
        return domainChannelCache.get(domain);
    }

    public static void remove(String domain) {
        if (ObjectUtil.isEmpty(domain)) {
            return;
        }
        domainChannelCache.remove(domain);
    }

}
